<?php
namespace PsiFramework\Classes\DAO;

import("Classes.DAO.AbstractDAO");
import("Classes.Util.Database.SqlQuery");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Classes\Util\Database\SqlQuery;
use \PsiFramework\Classes\Util\Database\Builder\QueryBuilder;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlTable;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The LanguageDAO class is the Data Access Object for the Languages
 *
 * @package DAO
 */
class LanguageDAO extends AbstractDAO
{

    protected static $_singleton = null;

    /**
     * Returns the array with all localized texts
     *
     * @param     string        $lang    the language to retrieve
     * @return    string[]    the dictionary of localized texts
     */
    public function getTexts(/*string*/ $lang)
    {

        $queryBuilder = new QueryBuilder();
        $queryBuilder->createSelectQuery();

        $table = new SqlTable("text", "psiframework");
        $queryBuilder->setTable($table);

        $keyAlias = "keytext" ;
        $langAlias = "lang" ;

        $columnOne = new SqlColumn("key", $table, null, $keyAlias);
        $queryBuilder->addSelectColumn($columnOne);

        $columnTwo = new SqlColumn($lang, $table, null, $langAlias);
        $queryBuilder->addSelectColumn($columnTwo);

        $query = $queryBuilder->buildQuery();
        $result = $this->_database->executeQuery($query);
        $texts = array();
        if ($result) {
            while (
                (
                    $array = $this->_database->getNextResult($result, false)
                ) != null
            ) {
                $texts[$array[$keyAlias]] = $array[$langAlias] ;
            }
        }
        return $texts;
    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return LanguageDAO instance
     */
    public static function getInstance()
    {

        if (self::$_singleton==null) {
            self::$_singleton = new LanguageDAO();
        }

        return self::$_singleton;
    }
}
